package com.example.mobile2030606148tdh0117hw;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;

public class MyDataAdapter {
    private  MySQLiteOpenHelper openHelper;
    private SQLiteDatabase database;
    public void openDB(Context context){
        openHelper = new MySQLiteOpenHelper(context, "factory.db", null, 1);
        try {
            database=openHelper.getWritableDatabase();
        }catch (Exception e){
            database=openHelper.getReadableDatabase();
        }
    }
    public void closeDB(){
        if (database!=null){
            database.close();
            database=null;
        }
        if (openHelper!=null){
            openHelper.close();
            openHelper=null;
        }
    }
    public List<Users> QueryALL(Context context){
        List<Users> usersList=new ArrayList<>();
        openDB(context);
        Cursor cursor=database.query("staff",null,null,null,null,null,null);
        if (cursor.moveToFirst()){
            do {
                Log.i("TAG","QueryALL");
                String name=cursor.getString(1);
                String skill=cursor.getString(2);
                int money=cursor.getInt(3);
                usersList.add(new Users(name,skill,money));
            }while (cursor.moveToNext());
        }
        cursor.close();
        closeDB();
        return usersList;
    }
}
